package com.woniu.common;

import com.woniu.enumeration.ResultCode;
import com.woniu.exception.BookBusinessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * */
@RestControllerAdvice
public class GlobalException {
    /**
     * 处理自定义业务异常
     * */
    @ExceptionHandler(BookBusinessException.class)
    public ResponseEntity<String> handleBusinessException(BookBusinessException e) {
        // 获取异常 错误码 和 信息
        ResultCode resultCode = e.getResultCode();

        // 返回错误信息
        return ResponseEntity.status(resultCode.getCode()).body(resultCode.getMessage());

    }

    /**
     * 处理未知异常
     * */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception e) {
        e.printStackTrace();

        // 返回错误信息
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());

    }


}
